package com.example.proyyj.Controller;


import com.example.proyyj.Entity.Building;
import com.example.proyyj.Entity.CollectionBuilding;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.CollectionBuildingService;
import com.example.proyyj.Util.JSONTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/CollectionBuilding/")//请求路径
public class CollectionBuildingController {
    @Resource
    CollectionBuildingService collectionBuildingService;
    ResultEntity resultEntity=null;

    /**
     * 是否收藏
     * @param UserId
     * @param BuildingId
     * @return
     */
   @GetMapping("collectionBuilding")
    private String collectionBuilding(@RequestParam("UserId") int UserId,@RequestParam("BuildingId")int BuildingId){
       resultEntity = new ResultEntity();

       try{
           CollectionBuilding collection=collectionBuildingService.collectionBuilding(UserId,BuildingId);
           if (collection !=null){
               resultEntity.setCode(0);
               resultEntity.setMsg("已收藏！");
           }else {
               resultEntity.setCode(2);
               resultEntity.setMsg("未收藏！");
           }

       }catch (Exception e){
           e.printStackTrace();
           resultEntity.setCode(999);
           resultEntity.setMsg("系统错误，请联系管理员");
       }
       return JSONTool.getJsonTool().toJson(resultEntity);
   }

    /**
     * 添加收藏
     * @param UserId
     * @param BuildingId
     * @return
     */
        @PostMapping("addBuilding")
        private String addBuilding(@RequestParam("UserId") int UserId,@RequestParam("BuildingId") int BuildingId){
        resultEntity = new ResultEntity();

        try{
            if (collectionBuildingService.addBuilding(UserId,BuildingId)>0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("收藏失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误，请联系管理员");
        }
            return JSONTool.getJsonTool().toJson(resultEntity);
}

    /**
     * 取消收藏
     * @param UserId
     * @param BuildingId
     * @return
     */
    @DeleteMapping("delBuilding")
    private String delBuilding(@RequestParam("UserId") int UserId,@RequestParam("BuildingId") int BuildingId){
        resultEntity = new ResultEntity();

        try{
            if (collectionBuildingService.delBuilding(UserId,BuildingId)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("删除失败");
            }


        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 查看收藏
     * @param UserId
     * @return
     */
    @GetMapping("lookBuildingList")
    private  String lookchiawareList(@RequestParam("UserId") int UserId){
        resultEntity = new ResultEntity();
        try{
            resultEntity = new ResultEntity();
            List<CollectionBuilding> list =collectionBuildingService.lookBuildinglist(UserId);
            if ( list !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查看成功");
                resultEntity.setData(list);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("查看失败");
            }

        }catch (Exception e){

            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }



    @GetMapping("lookBuildingid")
    private String lookBuilding(@RequestParam("BuildingId") int BuildingId){
        resultEntity = new ResultEntity();
        try{
            Building collection=collectionBuildingService.lookBuilding(BuildingId);

            if (collection !=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("查询成功");
                resultEntity.setData(collection);
            }else {
                resultEntity.setCode(2);
                resultEntity.setMsg("未查询成功");
            }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }
}
